const {Router} = require('express')

const router = Router()

const fs = require('fs')

router.get('/get-position', (req, res) => {
    fs.readFile('data/position.json', (error, data) => {
            if (error) throw error
            // console.log(JSON.parse(data))
            res.send(JSON.parse(data))
        }
    )
})
router.get('/get-nav', (req, res) => {
    fs.readFile('data/nav.json', (error, data) => {
            if (error) throw error
            // console.log(JSON.parse(data))
            res.send(JSON.parse(data))
        }
    )
})
router.get('/get-merchants', (req, res) => {
    fs.readFile('data/merchants.json', (error, data) => {
            if (error) throw error
            // console.log(JSON.parse(data))
            res.send(JSON.parse(data))
        }
    )
})
router.get(`/get-category`, (req, res) => {
    fs.readFile('data/merchants.json', (error, data) => {
            if (error) throw error
            let category = []
            for (let i = 0; i < JSON.parse(data).length; i++) {
                if (JSON.parse(data)[i].category.includes(req._parsedUrl.query)) {
                    category.push(JSON.parse(data)[i])
                }
            }
            res.send(category)
        }
    )
})
router.get(`/get-selected`, (req, res) => {
    fs.readFile('data/heJianLvRouHuoShao.json', (error, data) => {
            if (error) throw error
            let option = []
            let id = req._parsedUrl.query.split(',')
            for (let j = 0; j < id.length; j++) {
                for (let i = 0; i < JSON.parse(data).length; i++) {
                    if (JSON.parse(data)[i].id == id[j]) {
                        option.push(JSON.parse(data)[i])
                    }
                }
            }
            res.send(option)

        }
    )
})
router.get('/he-jian-lv-rou-huo-shao', (req, res) => {
    fs.readFile('data/heJianLvRouHuoShao.json', (error, data) => {
            if (error) throw error
            // console.log(JSON.parse(data))
            res.send(JSON.parse(data))
        }
    )
})


module.exports = router
